<?php

namespace MLM\Controller;

use Zend\View\Model\ViewModel;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;

class ReportController extends \MLM\Model\Generic\Controller {

    
    public function indexAction() {
        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get accessed menu
        $this->getAccessedMenu($identity->member_type_id, $identity->member_id);

        return new ViewModel();
    }


    public function mutasitransAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        $date1 = date('Ymd', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date2 = date('Ymd');

        $dateTmp1 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $dateTmp2 = date('Y-m-d');
        
        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);

        $dataSaldo = $accStorage->getTotalSaldoByPeriod("", "", "", $date1, $date2);

        $transStorage = new \MLM\Model\Transaction\Storage($this->_db);

       
        $transType = $transStorage->getTransType();

        //print_r($transType); die();

        //Get accessed menu
        $this->getAccessedMenu($identity->member_type_id, $identity->member_id);


        $view = new ViewModel();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            })
        })
        ");
        
        $view->setVariable('transType', $transType);
        $view->setVariable('dateTmp1', $dateTmp1);
        $view->setVariable('dateTmp2', $dateTmp2);
        $view->setVariable('dataSaldo', $dataSaldo);
        //$view->setTerminal(true);
        return $view;

    }


    public function mutasitransmonthlyAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        $month = date('Y-m');

        //print_r($transType); die();

        //Get accessed menu
        $this->getAccessedMenu($identity->member_type_id, $identity->member_id);


        $view = new ViewModel();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            })
        })
        ");

        $view->setVariable('month', $month);
        //$view->setTerminal(true);
        return $view;

    }

    
    public function totalsaldoAction(){
        $getParams = $this->getRequest()->getQuery();

        $transType = (isset($getParams['transType'])) ? $getParams['transType'] : "";
        $accNum = (isset($getParams['accNum'])) ? $getParams['accNum'] : "";
        $date1 = (isset($getParams['date1'])) ? str_replace('-', '', $getParams['date1']) : date('Ymd', mktime(0, 0, 0, date("m")  , date("d")-7, date("Y")));
        $date2 = (isset($getParams['date2'])) ? str_replace('-', '', $getParams['date2']) : date('Ymd');

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);
        
        $dataSaldo = $accStorage->getTotalSaldoByPeriod("", $accNum, $transType, $date1, $date2);
        
        $tmp = '<table cellpadding="5px">
                    <tr>
                        <td>Total Kredit</td>
                        <td>: </td>
                        <td align="right"> '.$dataSaldo['kredit'] .'</td>
                    </tr>
                    <tr>
                        <td>Total Debet</td>
                        <td>: </td>
                        <td align="right"> '.$dataSaldo['debet'] .'</td>
                    </tr>
                    <tr>
                        <td>Saldo Akhir</td>
                        <td>: </td>
                        <td align="right"> '.$dataSaldo['saldo'] .'</td>
                    </tr>
            </table>';

            
        $view = new ViewModel();
        $view->setTerminal(true);
        $view->setVariable('data', $tmp);
        return $view;
    }


    public function saldoAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        $accStorage = new \MLM\Model\Member\Account\Storage($this->_db);

        $dataSaldo = $accStorage->getTotalSaldoMember();

        //print_r($transType); die();

        //Get accessed menu
        $this->getAccessedMenu($identity->member_type_id, $identity->member_id);


        $view = new ViewModel();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            })
        })
        ");
        
        $view->setVariable('dataSaldo', $dataSaldo);
        //$view->setTerminal(true);
        return $view;

    }


    public function producttransAction()
    {

        $identity = $this->hasSession();

        if ($identity->member_type_id == 4)
            $this->layout('layout/admin');
        else
            $this->layout('layout/member');
            

        $date1 = date('Ymd', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date2 = date('Ymd');

        $dateTmp1 = date('Y-m-d', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $dateTmp2 = date('Y-m-d');


        
        //Get accessed menu
        $this->getAccessedMenu($identity->member_type_id, $identity->member_id);


        $view = new ViewModel();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            })
        })
        ");


        if ($identity->member_type_id == 4)
            $view->setVariable('accnum', '');
        else
            $view->setVariable('accnum', $identity->member_code);

        $view->setVariable('dateTmp1', $dateTmp1);
        $view->setVariable('dateTmp2', $dateTmp2);
        

        
        
        //$view->setTerminal(true);
        return $view;

    }


    //Get accessed menu
    private function getAccessedMenu($memberTypeId, $memberId){

        $notifStorage = new \MLM\Model\Notification\Storage($this->_db);
        $notifMsg = new \MLM\Model\Notification($notifStorage);
        $newMsgCnt = $notifMsg->getCountNewMsg($memberId);
        $newMsgList = $notifMsg->getNewMsgList($memberId);
        $newAlertCnt = $notifMsg->getCountAlertApproval($memberTypeId, $memberId);
        $newAlertList = $notifMsg->getAlertApproval($memberTypeId, $memberId);

        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        $control = $this->getEvent()->getRouteMatch()->getParam('controller');

        $this->layout()->menu = $control.'\\'.$action;
        $this->layout()->newMsgCnt = $newMsgCnt;
        $this->layout()->newMsgList = $newMsgList;
        $this->layout()->newAlertCnt = $newAlertCnt;
        $this->layout()->newAlertList = $newAlertList;

    }

    /*
    private function hasSession() {
        $auth = new AuthenticationService();

        $identity = $auth->getIdentity();

        if (!$auth->hasIdentity() || $identity->member_type_id <> 4) {
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }
     * 
     */


    private function hasSession() {
        $auth = new AuthenticationService();

        //$identity = $auth->getIdentity();

        if (!$auth->hasIdentity()) {
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }

}
